﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

namespace UserInterfaceLayer.User
{
    public partial class RequestRoom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strRequestID = Request.QueryString["RequestID"];
            string strRoomID = Request.QueryString["RoomID"];
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            int RoomID;
            if (!int.TryParse(strRoomID, out RoomID))
                Response.Redirect("~/");
            int RequestID;
            if (!int.TryParse(strRequestID, out RequestID))
                Response.Redirect("~/");
            DataAccessLayer.RequestRoom RequestRoom = DataAccessLayer.RequestRoom.FindByRequestIDRoomID_IncludeDeleted(db, RequestID, RoomID);
            if (RequestRoom == null)
                Response.Redirect("~/");
            FromDatabase(RequestRoom);
            btnDelete.Visible = true;
            lvHistory.DataSource = BusinessLogicLayer.DataObject.RequestRoom.Convert(DataAccessLayer.RequestRoom.HistoryToListByRequestIDRoomID(db, RequestID, RoomID));
            lvHistory.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRequestID = Request.QueryString["RequestID"];
                string strRoomID = Request.QueryString["RoomID"];
                int RequestID = int.Parse(strRequestID);
                int RoomID = int.Parse(strRoomID);
                DateTime OfficialStartTimestamp, OfficialEndTimestamp;
                if (!PersianDateTimeExtension.TryParseHijriSolar(txtOfficialStartTimestamp.Text, out OfficialStartTimestamp) ||
                        !PersianDateTimeExtension.TryParseHijriSolar(txtOfficialEndTimestamp.Text, out OfficialEndTimestamp))
                {
                    lblInvalidInput.Visible = true;
                    return;
                }
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.RequestRoom RequestRoom = DataAccessLayer.RequestRoom.FindByRequestIDRoomID_IncludeDeleted(db, RequestID, RoomID);
                DataAccessLayer.RequestRoom NewRequestRoom = new DataAccessLayer.RequestRoom();
                NewRequestRoom.RequestID = RequestID;
                NewRequestRoom.RoomID = RoomID;
                NewRequestRoom.EditorUserID = UserID;
                NewRequestRoom.Timestamp = DateTime.Now;
                ToDatabase(NewRequestRoom);
                db.RequestRooms.InsertOnSubmit(NewRequestRoom);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRequestID = Request.QueryString["RequestID"];
                string strRoomID = Request.QueryString["RoomID"];
                int RequestID = int.Parse(strRequestID);
                int RoomID = int.Parse(strRoomID);
                DateTime OfficialStartTimestamp, OfficialEndTimestamp;
                if (!PersianDateTimeExtension.TryParseHijriSolar(txtOfficialStartTimestamp.Text, out OfficialStartTimestamp) ||
                        !PersianDateTimeExtension.TryParseHijriSolar(txtOfficialEndTimestamp.Text, out OfficialEndTimestamp))
                {
                    lblInvalidInput.Visible = true;
                    return;
                }
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.RequestRoom RequestRoom = DataAccessLayer.RequestRoom.FindByRequestIDRoomID_IncludeDeleted(db, RequestID, RoomID);
                DataAccessLayer.RequestRoom NewRequestRoom = new DataAccessLayer.RequestRoom();
                NewRequestRoom.RequestID = RequestID;
                NewRequestRoom.RoomID = RoomID;
                NewRequestRoom.EditorUserID = UserID;
                NewRequestRoom.Timestamp = DateTime.Now;
                NewRequestRoom.IsDeleted = true;
                ToDatabase(NewRequestRoom);
                db.RequestRooms.InsertOnSubmit(NewRequestRoom);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.RequestRoom RequestRoom)
        {
            ddlStatus.SelectedValue = RequestRoom.RequestRoomStatusID.ToString();
            txtOfficialStartTimestamp.Text = RequestRoom.OfficialStartTimestamp.ToHijriSolarString();
            txtOfficialEndTimestamp.Text = RequestRoom.OfficialEndTimestamp.ToHijriSolarString();
            txtDescription.Text = RequestRoom.Description;
            lblDeletedItem.Visible = RequestRoom.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.RequestRoom RequestRoom)
        {
            RequestRoom.RequestRoomStatusID = int.Parse(ddlStatus.SelectedValue);
            RequestRoom.OfficialStartTimestamp = PersianDateTimeExtension.ParseHijriSolar(txtOfficialStartTimestamp.Text);
            RequestRoom.OfficialEndTimestamp = PersianDateTimeExtension.ParseHijriSolar(txtOfficialEndTimestamp.Text);
            RequestRoom.Description = txtDescription.Text;
        }
    }
}